package basic;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

class House implements Serializable{

	private static final long serialVersionUID = 1L;
	
}

class People implements Serializable{
	
	private static final long serialVersionUID = 1L;
	
	private String name;
	
	private House house;
	
	public People(String name,House house){
		this.name = name;
		this.house = house;
	}
	
	public void setName(String name){
		this.name = name;
	}
	
	private void writeObject(ObjectOutputStream os) throws IOException{
		os.defaultWriteObject();
	}
	
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "name[" + name  + "]" + super.toString() + " " + house.toString();
	}
}

public class MyWorld {

	public static void main(String[] args) throws Exception {
		
		System.setProperty("sun.io.serialization.extendedDebugInfo", "true");
		
		House house = new House();
		People p1 = new People("p1", house);
		People p2 = new People("p2", house);
		People p3 = new People("p3", house);
//		List<People> plist = Arrays.asList(p1,p2,p3);
//		System.out.println(plist);
		
		ByteArrayOutputStream bytes1 = new ByteArrayOutputStream();
		ObjectOutputStream os = new ObjectOutputStream(bytes1);
//		os.writeObject(plist);

		System.out.println(p2);
		os.writeObject(p2);
		
		p2.setName("p22");
		System.out.println(p2);
//		plist.set(1,p2);
//		os.writeObject(plist);
//		System.out.println(plist);
		os.writeObject(p2);
		

		p2 = new People("p3", new House());
		System.out.println(p2);
//		plist.set(1,p2);
//		os.writeObject(plist);
//		System.out.println(plist);
		
		os.writeObject(p2);
		
		System.out.println("--------------------");
 
		
//		ByteArrayOutputStream bytes2 = new ByteArrayOutputStream();
//		ObjectOutputStream os2 = new ObjectOutputStream(bytes1);
//		os.writeObject(plist);
		
		ObjectInputStream is1 = new ObjectInputStream(new ByteArrayInputStream(bytes1.toByteArray()));
//		System.out.println((List)is1.readObject());
		System.out.println((People)is1.readObject());
//		System.out.println((List)is1.readObject());
		System.out.println((People)is1.readObject());
//		System.out.println((List)is1.readObject());
		System.out.println((People)is1.readObject());
		
 
		
	}
}
